/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DataSelection;

import DataStructures.Property;
import java.util.ArrayList;

/**
 *
 * @author Manuel
 */
public class QueryBuilder {

    public String getQuery(ArrayList<Property> c1,ArrayList<Property> c2){
            String query = "SELECT * FROM (";

            query += this.createQuery(c1);
            query += ") as c1 JOIN (";
            query += this.createQuery(c2);
            query += ") as c2 USING (project_id);";

            return query;
    }

    public String createQuery(ArrayList<Property> properties){

        //CREATE A QUERY FOR PROJECT LEVEL

        Query projectQuery = new Query();
        projectQuery.setSelect("project_id");
        projectQuery.setFrom("project");
        projectQuery.setGroup("project_id");

        for(int i = 0;i<properties.size();i++){

            Property current = properties.get(i);

            //insert select
            if(!projectQuery.getSelect().contains(current.getQuery().getSelect())){
                projectQuery.setSelect(projectQuery.getSelect()+","+current.getQuery().getSelect());
            }

            //insert from
            if(!projectQuery.getFrom().contains(current.getQuery().getFrom())){

                    projectQuery.setFrom(projectQuery.getFrom()+" JOIN "+current.getQuery().getFrom()+ " USING (project_id)");


            }

            //insert where
            if(!projectQuery.getWhere().contains(current.getQuery().getWhere())){
                if(projectQuery.getWhere() != null && !projectQuery.getWhere().equals(""))
                    projectQuery.setWhere(projectQuery.getWhere()+ " AND "+current.getQuery().getWhere());
                else
                    projectQuery.setWhere(current.getQuery().getWhere());

            }

            //insert group by
            if(!projectQuery.getGroup().contains(current.getQuery().getGroup())){
                projectQuery.setGroup(projectQuery.getGroup()+ "," +current.getQuery().getGroup());
            }

            //insert having
            if(!projectQuery.getHaving().contains(current.getQuery().getHaving())){
                if(projectQuery.getHaving() != null && !projectQuery.getHaving().equals(""))
                    projectQuery.setHaving(projectQuery.getHaving()+ " AND "+current.getQuery().getHaving());
                else
                    projectQuery.setHaving(current.getQuery().getHaving());

            }


        }

        return projectQuery.generateString();

    }

    private String printOutProperties(ArrayList<Property> properties) {
        String output = "";
        output += "HAVE TO QUERY FOR THE FOLLOWING PROPERTIES <br/>";
        for(int i = 0;i<properties.size();i++){
            Property actual = properties.get(i);
            output += ("<br/>Concept: "+actual.getConcept()+" - Name: "+actual.getName()+" - Granularity: "+actual.getGranularity()+ " - Optional: "+actual.isOptional());
        }
        return output;
    }

    private String[] getQueryAliases(int n){
        String[] aliases = new String[n];
        for(int i = 0;i<n;i++){
            aliases[i] = "q"+i;
        }
        return aliases;
    }

    private String processFrom(String oldFrom, String addFrom) {

        String newFrom = "";
        
        //tokenize
        String[] oldTokens = oldFrom.split(" ");
        String[] addTokens = addFrom.split(" ");

        //clean tokens into two lists
        ArrayList<String> oldTokenList = new ArrayList<String>();
        ArrayList<String> addTokenList = new ArrayList<String>();
        for(String current : oldTokens){
            if(!(current.toLowerCase().equals("using") || current.toLowerCase().equals("join") || (current.toLowerCase().startsWith("(") && current.toLowerCase().endsWith(")"))))
                oldTokenList.add(current);
        }
        for(String current : addTokens){
            if(!(current.toLowerCase().equals("using") || current.toLowerCase().equals("join") || (current.toLowerCase().startsWith("(") && current.toLowerCase().endsWith(")"))))
                addTokenList.add(current);
        }

        //now process the new additional from clause
        String newAddFrom = "";
        for(String token : addTokenList){

            //check if there is a duplicate token
            if(oldTokenList.contains(token)){

                //check if there is an inner join clause
                if(addFrom.toLowerCase().contains("using")){

                    //get the other token and add the using clause
                    String otherToken ="";
                    for(String inT : addTokenList){
                        if(!token.equals(inT))
                            otherToken = inT;
                    }
                    String using = addFrom.toLowerCase().substring(addFrom.indexOf("using"));
                    newAddFrom = otherToken + " " + using;

                }
                //if it is no join, no new from clause is needed,return empty string
                else{
                    return "";
                }
            }
            //if there is no duplicate, add it.
            else{

            }
        }


        return oldFrom+ " " +newAddFrom;
    }
}
